/**
 * Project: push-center-biz-core
 * 
 * File Created at 2014年10月9日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.push.core.pushcenter.impl;

import javax.annotation.Resource;

import com.greenline.push.core.exception.PushCenterException;
import com.greenline.push.core.pushcenter.CacheCategoryManager;
import com.greenline.push.core.pushcenter.CategoryManager;
import com.greenline.push.dal.pushcenter.CategoryDAO;
import com.greenline.push.dal.pushcenter.dataobject.CategoryDO;
import com.greenline.push.dal.query.CategoryQuery;

/**
 * @Type CategoryManagerImpl
 * @Desc 
 * @author zhaoli
 * @date 2014年10月9日
 * @Version V1.0
 */
public class CategoryManagerImpl implements CategoryManager {

	@Resource
	private CategoryDAO categoryDAO;
	@Resource
	private CacheCategoryManager cacheCategoryManager;
	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.CategoryManager#insert(com.greenline.push.dal.pushcenter.dataobject.CategoryDO)
	 */
	@Override
	public Long insert(CategoryDO category) throws PushCenterException {
		if(categoryDAO.getBycategoryName(category.getCategoryName()) != null){
			throw new PushCenterException("分类名称已经存在");
		}
		Long lo = categoryDAO.insert(category);
		if(lo > 0){
			cacheCategoryManager.reloadCache();
		}
		return lo;
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.CategoryManager#getCategoryById(java.lang.Long)
	 */
	@Override
	public CategoryDO getCategoryById(Long id) {
		return categoryDAO.getCategoryById(id);
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.CategoryManager#update(com.greenline.push.dal.pushcenter.dataobject.CategoryDO)
	 */
	@Override
	public boolean update(CategoryDO category) throws PushCenterException {
		boolean suc = false;
		CategoryDO object = categoryDAO.getBycategoryName(category.getCategoryName());
		if(object != null && object.getId() != category.getId()){
			throw new PushCenterException("分类名称已经存在。");
		}
		if(categoryDAO.update(category) > 0){
			cacheCategoryManager.reloadCache();
			suc = true;
		}
		return suc;
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.CategoryManager#listByQuery(com.greenline.push.dal.query.CategoryQuery)
	 */
	@Override
	public CategoryQuery listByQuery(CategoryQuery query) {
		return categoryDAO.listByQuery(query);
	}

	/* (non-Javadoc)
	 * @see com.greenline.push.core.pushcenter.CategoryManager#listByQuery(java.lang.Long)
	 */
	@Override
	public boolean delete(Long id) {
		return categoryDAO.delete(id) > 0;
	}
}
